import { Howl, Howler } from 'howler';
/*
 * sounds:[{
    src:['sound.mp3']
    howl:{}
 }]
 */
export default class AUDIO {
    constructor(sounds) {
        if (sounds.length > 0) {
            this.sounds = sounds;
            this.id0 = null;
            this.id1 = null;
            this.init();
        }
    }
    // 
    // 初始化
    init() {
        if (this.sounds.length > 0) {
            this.sounds.forEach(sound => {
                let howl = null
                let src = sound.src;
                if (src.length > 0) {
                    howl = new Howl({
                        src: src
                    });
                }

                sound.howl = howl
            });
        }
    }
    // 播放
    play() {
        if (this.sounds.length > 0) {
            this.sounds.forEach((sound, index) => {
                this['id' + index] = sound.howl.play();
            })
        }
    }
    /*
        根据进度播放台词
    */
    // 进度 
    seek() {
        let seek = this.sounds[1].howl.seek()
        return seek
    }
    // 获取总时长
    duration() {
        // console.log(this.sounds[1].howl);
       return this.sounds[1].howl.duration()
    }
    end(callback) {
        this.sounds[1].howl.on('end', function () {
            callback()
        });
    }
}